package com.visec.hdy.task;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import com.visec.hdy.config.PCVHandle;
import com.visec.hdy.sync.ftp.code.FtpFileManager;
import com.visec.hdy.sync.ftp.code.FtpGetManager;
import com.visec.hdy.sync.ftp.code.FtpSendManager;
import com.visec.hdy.sync.ftp.code.GetManager;
import com.visec.hdy.sync.ftp.config.FtpClientModel;
import com.visec.hdy.sync.ftp.config.FtpClientModelCV;
import com.visec.hdy.sync.ftp.config.FtpSendClient;
import com.visec.hdy.sync.ftp.config.SyncGetConfig;
import com.visec.hdy.sync.ftp.config.SyncSendConfig;
import com.visec.hdy.transmission.ftp.config.FtpModel;
import com.visec.hdy.transmission.ftp.config.FtpModelCV;
import com.visec.hdy.transmission.rmi.RMIFactory;
import com.visec.hdy.transmission.rmi.code.RMINotify;
import com.visec.hdy.transmission.rmi.code.RMIServer;
import com.visec.hdy.transmission.rmi.config.RMIModel;
import com.visec.hdy.transmission.socket.config.SocketModel;
import com.visec.hdy.transmission.socket.config.TSocketModelCV;

public class ServerTaskReceive extends Thread {
	private Map<String, GetManager> map;
	private RMIModel rmiModel;
	private int defaultSize;
	private Map<String, FtpSendManager> clientMap;

	public ServerTaskReceive(Map<String, GetManager> map, RMIModel rmiModel, int defaultSize) {
		this.map = map;
		this.rmiModel = rmiModel;
		this.defaultSize = defaultSize;
		this.clientMap = new HashMap<String, FtpSendManager>();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		RMIServer createServer = RMIFactory.getInstance().createServer(rmiModel);
		createServer.setNotify(new RMINotify() {

			@Override
			public boolean notify(Remote t) {
				// TODO Auto-generated method stub
				ServerTaskInter serverTaskInter = (ServerTaskInter) t;
				try {
					Map<String, String> m = serverTaskInter.getSyncGetConfig();
					String action = serverTaskInter.getAction();
					String ftpId = m.get("-ftpId");
					if (ftpId == null) {
						System.out.println("no ftpId!");
						return true;
					}
					System.out.println("ftp action:" + action + ",ftpid:" + m.get("-ftpId"));
					if (action.equals("client_add")) {
						PCVHandle pcv = new PCVHandle(new TSocketModelCV(), new FtpModelCV(), new FtpClientModelCV());
						pcv.conversion(m);
						SocketModel socket = pcv.getTarget(SocketModel.class);
						FtpModel ftp = pcv.getTarget(FtpModel.class);
						FtpClientModel fcm = pcv.getTarget(FtpClientModel.class);

						if (socket.getIp() == null) {
							System.out.println("are you sb?please send server ip by -stip");
							return true;
						}
						if (socket.getPort() == 0) {
							System.out.println("are you 2b?please send server ip by -stport");
							return true;
						}

						SyncSendConfig fsc = new SyncSendConfig(fcm.getTools(), new FtpSendClient(ftp, socket),
								fcm.getSleepTime(), fcm.getFtpId(), socket.getBufferSize(), fcm.isSave());

						FtpSendManager instance = FtpSendManager.getInstance(fsc, socket);
						instance.setDaemon(true);
						System.out.println("start client,target server--stip:" + socket.getIp() + ",-stport:"
								+ socket.getPort() + ",-stcharset:" + socket.getCharset() + ",-stbufferSize:"
								+ socket.getBufferSize());
						instance.start();
						if (clientMap.containsKey(ftpId)) {
							FtpSendManager ftpSendManager = clientMap.remove(ftpId);
							
							ftpSendManager.shutdown();
						}
						
						clientMap.put(ftpId, instance);
					} else if (action.equals("client_del")) {
						if (clientMap.containsKey(ftpId)) {
							FtpSendManager ftpSendManager = clientMap.remove(ftpId);
							ftpSendManager.shutdown();
						}
					} else if (action.equals("server_add")) {
						PCVHandle pcv = new PCVHandle(new FtpModelCV());
						pcv.conversion(m);
						FtpModel ftp = pcv.getTarget(FtpModel.class);
						SyncGetConfig sgc = new SyncGetConfig(ftp, ftpId, new FtpFileManager(), defaultSize);
						map.put(ftpId, FtpGetManager.getInstance(sgc));
					} else if (action.equals("server_del")) {
						map.remove(ftpId);
					}
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
				return true;
			}
		});
	}

}
